/**
 * Title : jFamilyTree
 * Description: jFamilyTree
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
  */
package jFamilyTree.commons;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;

/**
 * Look and feel
 * 
 * @author Bernard Bou
 */
public class Laf
{
	/**
	 * Initialize look and feel
	 * 
	 * @param theseArgs
	 *        command line arguments
	 */
	static public void lookAndFeel(final String[] theseArgs)
	{
		// "com.incors.plaf.kunststoff.KunststoffLookAndFeel"
		// "com.jgoodies.looks.plastic.PlasticLookAndFeel"
		// "com.jgoodies.looks.windows.WindowsLookAndFeel"
		// "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"

		// look and feel
		String thisLaf = null;
		boolean customTheme = false;
		for (int i = 0; i < theseArgs.length; i++)
		{
			if (theseArgs[i].startsWith("laf=")) //$NON-NLS-1$
				thisLaf = theseArgs[i].substring(4);
			if (theseArgs[i].startsWith("theme=")) //$NON-NLS-1$
				customTheme = Boolean.parseBoolean(theseArgs[i].substring(6));
		}
		// theme
		if (customTheme)
			Laf.setCustomTheme(thisLaf);

		// bold
		Laf.setBold(false);

		// laf
		if (thisLaf != null && !thisLaf.isEmpty())
		{
			try
			{
				Laf.setLookAndFeel(thisLaf);
			}
			catch (UnsupportedLookAndFeelException e)
			{
				try
				{
					System.err.println("Unsupported LookAndFeel " + e.getMessage()); //$NON-NLS-1$
					String plafName = "javax.swing.plaf.metal.MetalLookAndFeel"; //$NON-NLS-1$
					UIManager.setLookAndFeel(plafName);
				}
				catch (Exception e2)
				{
					System.err.println("Can't set LookAndFeel"); //$NON-NLS-1$
				}
			}
		}
	}

	/**
	 * Set bold font
	 * 
	 * @param haveBold
	 *        true/false
	 */
	static public void setBold(boolean haveBold)
	{
		UIManager.put("swing.boldMetal", haveBold); //$NON-NLS-1$
	}

	/**
	 * Set look and feel
	 * 
	 * @param thisLafName
	 *        laf name
	 * @return true if successful
	 * @throws UnsupportedLookAndFeelException
	 */
	static public boolean setLookAndFeel(String thisLafName) throws UnsupportedLookAndFeelException
	{
		LookAndFeel thisLaf = getLaf(thisLafName);
		UIManager.setLookAndFeel(thisLaf);
		return true;
	}

	/**
	 * Get LAF instance
	 * 
	 * @param thisClassName
	 *        LAF class name
	 * @return instance of this class
	 */
	static LookAndFeel getLaf(String thisClassName)
	{
		try
		{
			Class<?> thisClass = Class.forName(thisClassName);
			Class<?>[] theseArgsClass = new Class[] {};
			Object[] theseArgs = new Object[] {};

			Constructor<?> thisConstructor = thisClass.getConstructor(theseArgsClass);
			Object thisInstance = thisConstructor.newInstance(theseArgs);
			return (LookAndFeel) thisInstance;
		}
		catch (ClassNotFoundException nfe)
		{
			nfe.printStackTrace();
		}
		catch (NoSuchMethodException nsme)
		{
			nsme.printStackTrace();
		}
		catch (InvocationTargetException ite)
		{
			ite.printStackTrace();
		}
		catch (IllegalAccessException iae)
		{
			iae.printStackTrace();
		}
		catch (InstantiationException ie)
		{
			ie.printStackTrace();
		}
		return null;
	}

	/**
	 * Set custom theme
	 * 
	 * @param thisLafName
	 *        laf name
	 * @return true if successful
	 * @throws UnsupportedLookAndFeelException
	 */
	static public boolean setCustomTheme(String thisLafName)
	{
		MetalTheme thisTheme = makeCustomTheme();
		if (thisTheme != null)
			setCurrentTheme(thisLafName, thisTheme);
		return true;
	}

	/**
	 * Set current theme
	 * 
	 * @param thisClassName
	 *        LAF class name (may have overridden 'setCurrentTheme()')
	 * @param thisTheme
	 *        theme
	 */
	static void setCurrentTheme(String thisClassName, MetalTheme thisTheme)
	{
		try
		{
			Class<?> thisClass = Class.forName(thisClassName);
			Class<?>[] theseArgsClass = new Class[] { MetalTheme.class };
			Method thisMethod = thisClass.getMethod("setCurrentTheme", theseArgsClass); //$NON-NLS-1$
			thisMethod.invoke(null, thisTheme);
			return;
		}
		catch (Exception e)
		{
			System.err.println("Setting current theme: " + e.getMessage()); //$NON-NLS-1$
			MetalLookAndFeel.setCurrentTheme(thisTheme);
		}
	}

	/**
	 * Make custom golden metal theme
	 * 
	 * @return metal theme
	 */
	static private MetalTheme makeCustomTheme()
	{
		return new DefaultMetalTheme()
		{
			private final javax.swing.plaf.ColorUIResource primary1 = new ColorUIResource(83, 74, 43);

			private final javax.swing.plaf.ColorUIResource primary2 = new ColorUIResource(154, 149, 131);

			private final javax.swing.plaf.ColorUIResource primary3 = new ColorUIResource(229, 227, 222);

			private final javax.swing.plaf.ColorUIResource secondary1 = new ColorUIResource(105, 97, 78);

			private final javax.swing.plaf.ColorUIResource secondary2 = new ColorUIResource(172, 168, 153);

			private final javax.swing.plaf.ColorUIResource secondary3 = new ColorUIResource(236, 233, 216);

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getPrimary1()
			 */
			public javax.swing.plaf.ColorUIResource getPrimary1()
			{
				return primary1;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getPrimary2()
			 */
			public javax.swing.plaf.ColorUIResource getPrimary2()
			{
				return primary2;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getPrimary3()
			 */
			public javax.swing.plaf.ColorUIResource getPrimary3()
			{
				return primary3;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getSecondary1()
			 */
			public javax.swing.plaf.ColorUIResource getSecondary1()
			{
				return secondary1;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getSecondary2()
			 */
			public javax.swing.plaf.ColorUIResource getSecondary2()
			{
				return secondary2;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.plaf.metal.MetalTheme#getSecondary3()
			 */
			public javax.swing.plaf.ColorUIResource getSecondary3()
			{
				return secondary3;
			}
		};
	}
}
